package com.lesama.file;

// 统计文件夹中文件类型的个数

import java.io.File;
import java.util.HashMap;

public class FileTest5 {

    static HashMap<String, Integer> hm = new HashMap<>();
    static int count = 0;

    public static void main(String[] args) {
        File directory = FileTest1.getDirectory();
        countFiles(directory);
        System.out.println(hm);
    }

    public static void countFiles(File directory) {
        File[] files = directory.listFiles();
        if(files == null) {
            return;
        }
        for (File file : files) {
            if (file.isFile()) {
                String s = file.getName();
                String[] parts = s.split("\\.");
                if(parts.length == 1) {
                    count++;
                } else {
                    String key = parts[parts.length - 1];
                    if(!hm.containsKey(key)) {
                        hm.put(key, 1);
                    } else {
                        hm.put(key, hm.get(key) + 1);
                    }
                }
            } else {
                countFiles(file);
            }
        }
    }
}